package com.sqx.modules.repairRecord.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.entity.UserMoney;
import com.sqx.modules.app.entity.UserMoneyDetails;
import com.sqx.modules.app.service.UserMoneyDetailsService;
import com.sqx.modules.app.service.UserMoneyService;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.orders.entity.Orders;
import com.sqx.modules.orders.service.OrdersService;
import com.sqx.modules.pay.controller.app.AliPayController;
import com.sqx.modules.pay.entity.PayDetails;
import com.sqx.modules.pay.service.PayDetailsService;
import com.sqx.modules.repairRecord.entity.RepairRecord;
import com.sqx.modules.repairRecord.dao.RepairRecordDao;
import com.sqx.modules.repairRecord.service.RepairRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.task.entity.HelpOrder;
import com.sqx.modules.task.service.HelpOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author www.javacoder.top
 * @since 2022-12-12
 */
@Service
public class RepairRecordServiceImpl extends ServiceImpl<RepairRecordDao, RepairRecord> implements RepairRecordService {
    @Autowired
    private HelpOrderService orderService;
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private AliPayController aliPayController;
    @Autowired
    private PayDetailsService detailsService;
    @Autowired
    private UserMoneyService userMoneyService;
    @Autowired
    private UserService userService;

    @Override
    public IPage<RepairRecord> getRecordList(Integer page, Integer limit, RepairRecord repairRecord) {
        Page<RepairRecord> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }
        IPage<RepairRecord> recordIPage = baseMapper.selectPage(pages, new QueryWrapper<>(repairRecord).orderByDesc("create_time"));
        for (RepairRecord record : recordIPage.getRecords()) {
            if (record.getOrderType() == 1) {
                record.setHelpOrder(orderService.getOne(new QueryWrapper<HelpOrder>().eq("order_no", record.getOrderNo())));
            } else {
                record.setOrders(ordersService.getOne(new QueryWrapper<Orders>().eq("orders_no", record.getOrderNo())));
            }
        }
        return recordIPage;
    }

    @Override
    public Result getOrderByRecord(Integer orderType, String orderNo) {
        if (orderType == 1) {
            return Result.success().put("data", orderService.getOne(new QueryWrapper<HelpOrder>().eq("order_no", orderNo)));
        } else {
            return Result.success().put("data", ordersService.getOne(new QueryWrapper<Orders>().eq("orders_no", orderNo)));
        }
    }

    @Override
    public Result refundRepair(Long userId, Long recordId) {
        PayDetails payDetails;
        RepairRecord repairRecord = baseMapper.selectById(recordId);
        UserMoney userMoney = userMoneyService.selectUserMoneyByUserId(userId);
        UserEntity userEntity = userService.getById(repairRecord.getUserId());
        UserEntity rideEntity = userService.getById(repairRecord.getRideId());


        if (userId.equals(repairRecord.getRideId())) {
            if (userMoney.getMoney().compareTo(repairRecord.getMoney()) >= 0) {
                userMoneyService.updateMoney(2, rideEntity.getUserId(), repairRecord.getMoney());
                if (repairRecord.getOrderType() == 1) {
                    HelpOrder helpOrder = orderService.getOne(new QueryWrapper<HelpOrder>().eq("order_no", repairRecord.getOrderNo()));
                    payDetails = detailsService.getOne(new QueryWrapper<PayDetails>().eq("relation_id", helpOrder.getId()).eq("state", 1));
                } else {
                    Orders orders = ordersService.getOne(new QueryWrapper<Orders>().eq("orders_no", repairRecord.getOrderNo()));
                    payDetails = detailsService.getOne(new QueryWrapper<PayDetails>().eq("relation_id", orders.getOrdersId()).eq("state", 1));
                }
                if (repairRecord.getPayWay() == 0) {
                    userMoneyService.updateMoney(1, userEntity.getUserId(), repairRecord.getMoney());
                    return null;
                } else if (repairRecord.getPayWay() == 1) {
                    aliPayController.alipayRefund(payDetails.getTradeNo(), BigDecimal.valueOf(payDetails.getMoney()));
                    return Result.success();
                } else if (repairRecord.getPayWay() == 2) {
                    return null;
                } else {
                    return null;
                }
            } else {
                return Result.error("余额不足,请充值后再进行操作");
            }
        } else {
            return Result.error("您无权操作");
        }
    }

    @Override
    public List<RepairRecord> getRecordByOrderNo(Integer orderType, String orderNo) {
        return baseMapper.selectList(new QueryWrapper<RepairRecord>().eq("order_no", orderNo).eq("order_type", orderType));
    }
}
